var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ifShowPasswordTips:false,//选中提示输入支付密码,false隐藏、true显示
    paymentType: 1,// 支付方式 1 线下 2 微信 3 银行卡 4 余额
    orderVo: {},//用于存储订单信息
    allTotal: 0,
    buthidden: false,
    bId:"",
    amount: "",//账户余额
    amountOk:true,//余额是否可用
    ifTrue: false,
    Length: 6,        //输入框个数
    isFocusOne: true,    //聚焦
    ValueOne: "",        //输入的内容
    ispassword: true, //是否密文显示 true为密文， false为明文。
    userVo:"",
    buthiddenV: false,
    ifShowV: true,
    ifbuthiddenV:true,
    flag: 0,
    statusSelect: "", //判断是否选中
    checks: [],
    comment: "匿名评价用户",
    arrayChecks: "",
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
    var that = this;
    var bId = options.bId;
    that.setData({ bId: options.bId,allTotal: options.allTotal});
    wx.request({
      method: "POST",
      url: 'https://www.zaishengwang.com/wx/toHomePriced.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        id: bId
      },
      success: function (res) {
       
        let createTime = res.data.orderVo.createTime.substring(0, 19);
        res.data.orderVo.createTime = createTime;
        that.setData({ orderVo: res.data.orderVo});
      },
      fail: function (error) {
        console.log(error);
      }

    })

  },
  //微信支付
  paymentWx: function(){
    var that = this;
    that.setData({ paymentType:2 })
  },
  //银行卡支付
  paymentUpper: function () {
    var that = this;
    that.setData({ paymentType:3})
  },
  //线下支付
  paymentLower: function(){
    var that = this;
    that.setData({ paymentType:1})
  },
  //余额支付
  paymentMoney: function () {
    var that = this;
    if (that.data.amountOk){
      that.setData({ paymentType: 4});
    //  that.onMineInfo();
    }
   
    
  },
  bindFormSubmit:function(){
    var that = this ;
    if (that.data.paymentType==4&&that.data.ValueOne == "") {
      // wx.showToast({
      //   title: '支付密码必填',
      //   icon: "none"
      // })
     // that.setData({ifShowPasswordTips: true, });
      that.onMineInfo();
      
    } else if (that.data.paymentType == 4 && that.data.ValueOne.length != 6 ) {
      wx.showToast({
        title: '密码长度有误',
        icon: "none"
      })
     // that.setData({ ifShowPasswordTips: true, });
      that.onMineInfo();
      
    } else{
    that.setData({ buthidden: true });
    wx.request({
      method: "POST",
      url: 'https://www.zaishengwang.com/wx/toUpdateStatus.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        id: that.data.bId,
        paymentType: that.data.paymentType, 
        countPrice: that.data.allTotal,
        password: that.data.ValueOne
      },
      success: function (res) {
        var paymentType = that.data.paymentType;
        var allTotal = that.data.allTotal;
        if(res.data.msg == "1"){
          var receiptContacts = res.data.result.userContacts;
          var receiptTelephone = res.data.result.userTelephone;
          var userId = res.data.result.userId;
          var sendUserId = res.data.result.receiptId;
          var orderId = res.data.result.orderNumber;
          
          wx.navigateTo({
            url: 'paymentSucc?userId=' + userId + '&sendUserId=' + sendUserId + '&orderId=' + orderId + '&receiptContacts=' + receiptContacts + '&receiptTelephone=' + receiptTelephone + '&paymentType=' + paymentType + '&allTotal=' + allTotal,
          })
         
        }else{
          if (res.data.msg == "2"){
          //   wx.showToast({
          //     title: '密码错误',
          //     icon: "none"
          //   })
          // }
          // that.onMineInfo();
            wx.showModal({
              title: '提示',
              content: '支付密码错误',
              cancelText:"忘记密码",
              confirmText:"重试",
              cancelColor:"#50BEC5",
              confirmColor:"#666",
              success(res) {
                if (res.confirm) {
                  that.onMineInfo();//重试
                } else if (res.cancel) {
                  that.goCheckSetPaymentPassword();//忘记密码
                }
              }
            })
          that.setData({ ValueOne:"", buthidden: false });
        }
        }  
      },
      fail: function (error) {
        console.log(error);
        that.setData({ buthidden: false });
      }

    })
    }
  },

  changeColor1: function () {
    var that = this;
    that.setData({
      ifbuthiddenV: false,
      flag: 1,
      checks: [{
        name: "货量少",
        value: '0',
        checked: false
      }, {
        name: "货质一般",
        value: '1',
        checked: false
      }, {
        name: "回收频率低",
        value: '2',
        checked: false
      }, {
        name: "不守信用",
        value: '3',
        checked: false
      }],
      comment: "一般用户"
    });
  },
  changeColor2: function () {
    var that = this;
    that.setData({
      ifbuthiddenV: false,
      flag: 2,
      checks: [{
        name: "货量少",
        value: '0',
        checked: false
      }, {
        name: "货质一般",
        value: '1',
        checked: false
      }, {
        name: "回收频率低",
        value: '2',
        checked: false
      }, {
        name: "不守信用",
        value: '3',
        checked: false
      }],
      comment: "一般用户"
    });
  },
  changeColor3: function () {
    var that = this;
    that.setData({
      flag: 3,
      ifbuthiddenV: false,
      checks: [{
        name: "货量较多",
        value: '0',
        checked: false
      }, {
        name: "货质优良",
        value: '1',
        checked: false
      }, {
        name: "回收有频率",
        value: '2',
        checked: false
      }, {
        name: "态度较好",
        value: '3',
        checked: false
      }],
      comment: "优质用户",
    });
  },
  changeColor4: function () {
    var that = this;
    that.setData({
      ifbuthiddenV: false,
      flag: 4,
      checks: [{
        name: "货量较多",
        value: '0',
        checked: false
      }, {
        name: "货质优良",
        value: '1',
        checked: false
      }, {
        name: "回收有频率",
        value: '2',
        checked: false
      }, {
        name: "态度较好",
        value: '3',
        checked: false
      }],
      comment: "优质用户",
    });
  },
  changeColor5: function () {
    var that = this;
    that.setData({
      ifbuthiddenV: false,
      flag: 5,
      checks: [{
        name: "货量大",
        value: '0',
        checked: false
      }, {
        name: "货质优",
        value: '1',
        checked: false
      }, {
        name: "回收频率高",
        value: '2',
        checked: false
      }, {
        name: "态度好",
        value: '3',
        checked: false
      }],
      comment: "VIP用户",
    });
  },

  clicks: function (e) {
   
    let index = e.currentTarget.dataset.index;
    let arrs = this.data.checks;
    let array = new Array;
    let text = e.currentTarget.dataset.text;
    if (arrs[index].checked == false) {
      arrs[index].checked = true;
      array.push(text);
    } else {
      arrs[index].checked = false;
    }
    this.setData({
      checks: arrs
    })

  },

  // 提交清空当前值
  bindSubmit: function () {
    var that = this;
    var checks = that.data.checks;
    var array = new Array;

    for (var i = 0; i < checks.length; i++) {
      if (checks[i].checked == true) {
        array.push(checks[i].name);
      }
    }
    that.setData({ arrayChecks: array })
    console.log(array);
    console.log(that.data.arrayChecks);
    if (array.length <= 0) {
      wx.showToast({
        title: '评价类别必选',
        icon: 'none',
        duration: 1500,
      })
      return;
    }
    that.setData({ buthiddenV: true });
    wx.request({
      method: "POST",
      url: 'https://www.zaishengwang.com/wx/toSaveEvaluate.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        userId: that.data.orderVo.userId,
        sendUserId: that.data.orderVo.receiptId,
        orderId: that.data.orderVo.orderNumber,
        score: that.data.flag,
        label: that.data.arrayChecks
      },
      success: function (res) {
        var c = 2;
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 1500,
         
        })
        var intervalId = setInterval(function () {
          c = c - 1;
          if (c == 0) {
            clearInterval(intervalId);
            that.setData({ ifShowV: true });
            wx.reLaunch({
              url: 'home?finish=1',
            })
          }
        }, 1000)
        
      },
      fail: function (error) {
      }
    })

  },
  
  //评价隐藏
  cancelV: function () {
    var that = this;
    that.setData({ ifShowV: true });
    wx.reLaunch({
      url: 'home?finish=1',
    })

  },

 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      method: "POST",
      url: 'https://www.zaishengwang.com/wx/serchUserAccount.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        bId: app.globalData.userId
      },
      success: function (res) { 
        var amount = res.data.accountVo.amount ;
        that.setData({ amount:amount });
        if (amount > that.data.allTotal){
          that.setData({ amountOk: true });
        }else{
          that.setData({ amountOk: false, paymentType: 1 });
        }
      },
      fail: function (error) {
        console.log(error);
      }

    })
    
  },

  onFocusOne: function (e) {
    var that = this;
    var inputValue = e.detail.value.trim();
    that.setData({
      ValueOne: inputValue,
    });
    if (e.detail.value.length >= 6) {
      that.setData({ ifShowPasswordTips: false });
      that.bindFormSubmit();
    }
  },

  onTapOne: function (e) {

    var that = this;
    that.setData({
      isFocusOne: true,
    })
   
  },

  onRightCancel:function(){
    var that = this;
    that.setData({ ValueOne:"", ifShowPasswordTips: false });
  },

  //忘记密码
  goCheckSetPaymentPassword: function () {
    var that = this;
    var wxType = that.data.userVo.wxType;
    var phone = that.data.userVo.telephone;
    wx.navigateTo({
      url: '../mine/checkSetPaymentPassword?wxType=' + wxType + '&phone=' + phone,
    });
  },

  onMineInfo: function () {
    var that = this;
    wx.request({
      method: "POST",
      url: 'https://www.zaishengwang.com/wx/toHomeUser.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        id: app.globalData.userId,
      },
      success: function (res) {
        
        var payStatus = res.data.userVo.payStatus;
        var wxType = res.data.userVo.wxType;
        var phone = res.data.userVo.telephone;
        var payStatus = res.data.userVo.payStatus;
        that.setData({ userVo:res.data.userVo});

        if (payStatus==1){
          that.setData({ ifShowPasswordTips: true })
        } else if (payStatus == 2){
           wx.navigateTo({
             url: '../mine/checkSetPaymentPassword?wxType=' + wxType + '&phone=' + phone,
           })
        }
       
      },
      fail: function (error) {
        console.log(error);
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})